package dao;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public abstract class AbstractFactory {
    
        private static AbstractFactory _instance;
    
        public static void setInstance(AbstractFactory factory) {
                if (_instance != null) // supprimer ce test si l'_instance peut varier
                        throw new RuntimeException("instance déjà enregistrée");
                _instance = factory;
        }

        public static AbstractFactory getInstance() {
                if (_instance == null)
                        throw new RuntimeException("il faut d'abord enregistrer une factory");
                return _instance;
        }

        public static void setInstance(Class factory) {
                if (!childOfFactory(factory)) throw new RuntimeException();
                Method method = null;
                try {
                        method = factory.getDeclaredMethod("enregistrer", new Class[0]);
                } catch (SecurityException e) {e.printStackTrace();
                } catch (NoSuchMethodException e) {throw new InternalError("il faut une méthode enregistrer");}
                try {
                        method.invoke(null,new Object[0]);
                } catch (IllegalArgumentException e) {e.printStackTrace();
                } catch (IllegalAccessException e) {throw new InternalError("il faut une méthode enregistrer public");
                } catch (InvocationTargetException e) {e.printStackTrace();}
        }
        
        private static boolean childOfFactory(Class factory) {
                Class parent = factory;
                while ((parent = parent.getSuperclass()) != Object.class)
                        if (parent == AbstractFactory.class) return true;
                return false;
        }
        
        public abstract AdresseDao getAdresseDao();
        public abstract ClientParticulierDao getClientParticulierDao();
        public abstract ClientSocieteDao getClientSocieteDao();
        public abstract CodePostalDao getCodePostalDao();
        public abstract EmployeDao getEmployeDao();
        public abstract InterventionDao getInterventionDao();
        public abstract TechnicienDao getTechnicienDao();
        public abstract ZoneDao getZoneDao();
        public abstract FormeDao getFormeDao();
}